package com.shisfish.library.Controller;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import net.sf.json.JSONArray;

import com.shisfish.library.Impl.BookAddMessageImpl;
import com.shisfish.library.Impl.BookImpl;
import com.shisfish.library.Impl.BookMessageImpl;
import com.shisfish.library.Impl.PublisherImpl;
import com.shisfish.library.Model.Book;
import com.shisfish.library.Model.BookAddMessage;
import com.shisfish.library.Model.BookMessage;
import com.shisfish.library.Model.Publisher;
import com.shisfish.library.Prere.Login;
import com.shisfish.library.Utils.ChangeISBNUtils;
import com.shisfish.library.Utils.ExcelToBean;
import com.shisfish.library.Utils.JsonValueUtil;
import com.shisfish.library.Utils.StringUtils;
import com.shisfish.library.Utils.FileUpload.FileUploadUtils;
import com.shisfish.library.Utils.FileUpload.FormJsonArrUtil;
import com.shisfish.library.Utils.Global.GlobalVariable;
import com.shisfish.library.Utils.Global.toJsonUtils;
import com.shisfish.library.Utils.PageModel.Page;
import com.shisfish.library.Utils.PageModel.PageUtils;

@Path("book")
public class BookController extends GlobalVariable {

	BookImpl bookImpl = new BookImpl();
	
	BookAddMessageImpl bookAddMessageImpl  = new BookAddMessageImpl();
	
	private BookMessageImpl bookMessageImpl = new BookMessageImpl();
	
	FileUploadUtils fileUploadUtils = new FileUploadUtils();

	JsonValueUtil jsonValueUtils = new JsonValueUtil();
	
	private PublisherImpl publisherImpl = new PublisherImpl();	
	/**
	 * @Author shisfish
	 * @Date 2016年1月5日 下午4:08:00
	 * @Descrition : 新增一个书类
	 * @param idadmin
	 * @param request
	 * @return
	 */
	@POST
	@Path("insertABook/{idadmin}")
	@Produces("application/json; charset=utf-8")
	public String insertABook(@PathParam("idadmin") String idadmin, @Context HttpServletRequest request) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		if (Login.isLoginAdmin(idadmin, request)) {
		
			JSONArray customjsArr;
			// 取得所有文件和表单值
			JSONArray jsArr = fileUploadUtils.getRiverWayUpload(request);
			// 取得所有普通表单值
			customjsArr = new FormJsonArrUtil().getFormJsonArr(jsArr, "form");
	
			// 获取form表单数据
			String isbn = jsonValueUtils.getValue(customjsArr, "isbn");
	
			String isbn10 = "";
			String isbn13 = "";
			isbn = isbn.replaceAll("-", "");
			if (isbn.length() == 10) {
				isbn10 = isbn;
				isbn13 = ChangeISBNUtils.getISBN13(isbn);
			} else {
				isbn13 = isbn;
			}
			if (isbn13.length() == 13) {
	
			String title = jsonValueUtils.getValue(customjsArr, "title");
			String subtitle = jsonValueUtils.getValue(customjsArr, "subtitle");
			String title_foreign = jsonValueUtils.getValue(customjsArr,
					"title_foreign");
			String author = jsonValueUtils.getValue(customjsArr, "author");
			String publisher = jsonValueUtils.getValue(customjsArr, "publisher");
			String pubaddress = jsonValueUtils.getValue(customjsArr, "pubaddress");
			String pubdate = jsonValueUtils.getValue(customjsArr, "pubdate");
			String pages = jsonValueUtils.getValue(customjsArr, "pages");
			String size = jsonValueUtils.getValue(customjsArr, "size");
			String keyword = jsonValueUtils.getValue(customjsArr, "keyword");
			String summary = jsonValueUtils.getValue(customjsArr, "summary");
			String category = jsonValueUtils.getValue(customjsArr, "category");
			String language = jsonValueUtils.getValue(customjsArr, "language");
			String price = jsonValueUtils.getValue(customjsArr, "price");
			String series_name = jsonValueUtils.getValue(customjsArr, "series_name");
			String enclosure = jsonValueUtils.getValue(customjsArr, "enclosure");
			String cn = jsonValueUtils.getValue(customjsArr, "cn");
			String period = jsonValueUtils.getValue(customjsArr, "period");
			String unit = jsonValueUtils.getValue(customjsArr, "unit");
			String classification = jsonValueUtils.getValue(customjsArr,
					"classification");
			String binding = jsonValueUtils.getValue(customjsArr, "binding");
			String translator = jsonValueUtils.getValue(customjsArr, "translator");
	
			int count = bookImpl.countByCategoryT(category);
			
			String code = category + "/" + (count + 1);
			
			
			Book book = new Book(code, isbn10, isbn13, title, subtitle,
					title_foreign, author, publisher, pubaddress, pubdate, pages,
					size, keyword, summary, category, language, price, series_name,
					enclosure, cn, period, unit, classification, binding,
					translator);
	
			int flag = bookImpl.insertOne(book);
	
			if (flag == 0) {
				globalMap.put(STATUS_CODE, INSERT_WR);
			} else {
				
				// 新增一本书类的时候 添加书的数量信息
				BookMessage bookMessage = new BookMessage(isbn13, 0, 0, 0, 0, 0);
				
				bookMessageImpl.insertOne(bookMessage);
				
				globalMap.put(STATUS_CODE, SUCCESS);
				
			}
			} else {
				globalMap.put(STATUS_CODE, ERROR);
				globalMap.put("message", "请输入13位的isbn!");
			}
		}
		else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		return toJsonUtils.map2Json(globalMap);

	}

	/**
	 * @Author shisfish
	 * @Date 2016年1月5日 下午4:11:44
	 * @Descrition : excel新增书籍
	 * @param idadmin
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@POST
	@Path("excelInsert/{idadmin}")
	@Consumes({ MediaType.MULTIPART_FORM_DATA, MediaType.APPLICATION_JSON })
	@Produces(MediaType.TEXT_PLAIN)
	public String excelInsert(@PathParam("idadmin") String idadmin, @Context HttpServletRequest request)
			throws Exception {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		if (Login.isLoginAdmin(idadmin, request)) {
		
			String filePath = "d:\\";
	
			fileUploadUtils.setFilePath(filePath);
	
			// 取得所有文件和表单值 并保存文件至本地
			JSONArray jsArr = fileUploadUtils.getRiverWayUpload(request);
	
			// 获取文件
			JSONArray fileJsArr = new FormJsonArrUtil().getFormJsonArr(jsArr,
					"file");
	
			// 取得文件保存路径 "xxx" 为前台表单name 值必须一样
			String excelFilePath = filePath
					+ jsonValueUtils.getValue(fileJsArr, "bookFile");
	
			// 将上传的表格转换成List
			String configFilePath = request.getSession().getServletContext()
					.getRealPath("/")
					+ File.separator + "excelModel/book.xml";
	
			List<Book> books = new ArrayList<Book>();
	
			ExcelToBean<Book> util = new ExcelToBean<Book>();
	
			try {
	
				// 判断文件格式是否正确
				books = util.parseExcelFileToBeans(new File(excelFilePath),
						new File(configFilePath));
				
				
				List<BookAddMessage> isbns = new ArrayList<BookAddMessage>();
				
				Publisher publisher = publisherImpl.getOne();
				
				for (Book book : books) {
					
					List<Book> bos = bookImpl.selectByKey(book.getIsbn13());
					if (bos == null || bos.size() == 0) {
						Publisher pub = publisherImpl.selectByName(book.getPublisher());
						if (pub != null) {
							book.setPublisher(pub.getId() + "");
						} else {
							book.setPublisher(publisher.getId() + "");
						}
						
						int count = bookImpl.countByCategoryT(book.getCategory());
						
						String code = book.getCategory() + "/" + (count + 1);
						book.setCode(code);
						
						bookImpl.insertOne(book);
						
						BookAddMessage bookAddMessage = new BookAddMessage();
						bookAddMessage.setAdder(idadmin);
						bookAddMessage.setIsbn(book.getIsbn13());
						bookAddMessage.setRemark("");
						isbns.add(bookAddMessage);
					}
				}
				
				/*try {
					int flag = bookImpl.insertAll(books);
	
					if (flag != 0) {
						globalMap.put("message", ADD_SUCCESS);
						
						int flag_1 = bookAddMessageImpl.insertAll(isbns);
						System.out.println(flag_1);
						
					} else {
						globalMap.put("message", ADD_ERROR);
					}
	
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
					globalMap.put("message", ADD_ERROR);
				}*/
	
			} catch (Exception e) {
				globalMap.put("message", ADD_ERROR);
			}
		}

		return toJsonUtils.map2Json(globalMap);

	}

	/**
	 * @Author shisfish
	 * @Date 2016年1月5日 下午4:12:40
	 * @Descrition : 获取全部的书类
	 * @return
	 */
	@GET
	@Path("selectAll")
	@Produces("application/json; charset=utf-8")
	public String selectAll() {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		List<Book> books = bookImpl.selectAll();
		if (books.size() > 0) {
			globalMap.put("list", books);
		}

		return toJsonUtils.map2Json(globalMap);
	}

	/**
	 * @Author shisfish
	 * @Date 2016年1月5日 下午4:13:15
	 * @Descrition : 同过中图法分类查询书类信息（分页）
	 * @param category
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	@GET
	@Path("selectByCategory/{category}/{currentPage}/{pageSize}")
	@Produces("application/json; charset=utf-8")
	public String selectByCategory(@PathParam("category") String category,
			@PathParam("currentPage") int currentPage,
			@PathParam("pageSize") int pageSize) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		int len = bookImpl.countByCategory(category);

		if (len > 0) {

			Page page = PageUtils.createPage(len, currentPage, pageSize);

			List<Book> books = bookImpl.selectByCategory(category,
					page.getBeginIndex(), pageSize);

			if (books.size() > 0) {
				globalMap.put("list", books);
				globalMap.put("page", page);
				globalMap.put(STATUS_CODE, SUCCESS);
			} else {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			}
		} else {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		}

		return toJsonUtils.map2Json(globalMap);
	}
	
	/**
	 * @Author shisfish
	 * @Date 2016年1月5日 下午4:14:02
	 * @Descrition : 通过条件查询书类信息（分页）
	 * @param type
	 * @param choose
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	@GET
	@Path("selectByCho/{type}/{choose}/{currentPage}/{pageSize}")
	@Produces("application/json; charset=utf-8")
	public String selectByCho(@PathParam("type") String type, @PathParam("choose") String choose, 
			@PathParam("currentPage") int currentPage, @PathParam("pageSize") int pageSize) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		List<Book> books = new ArrayList<Book>();
		
		Page page = new Page();
		int count = 0;
		switch(type) {
			case "title" : 
				count = bookImpl.countByTitle(choose);
				if(count > 0){
					page = PageUtils.createPage(count, currentPage, pageSize);
					books = bookImpl.selectByTitle(choose, page.getBeginIndex(), pageSize);
				}
				break;
			case "author" :
				count = bookImpl.countByAuthor(choose);
				if(count > 0){
					page = PageUtils.createPage(count, currentPage, pageSize);
					books = bookImpl.selectByAuthor(choose, page.getBeginIndex(), pageSize);
				}
				break;
			case "category" :
				count = bookImpl.countByCategory(choose);
				if(count > 0){
					page = PageUtils.createPage(count, currentPage, pageSize);
					books = bookImpl.selectByCategory(choose, page.getBeginIndex(), pageSize);
				}
				break;
			case "isbn" :
				count = bookImpl.countByISBN(choose);
				if(count > 0){
					page = PageUtils.createPage(count, currentPage, pageSize);
					books = bookImpl.selectByISBN1(choose, page.getBeginIndex(), pageSize);
				}
				break;
			case "publisher" :
				count = bookImpl.countByPublisher(choose);
				if(count > 0){
					page = PageUtils.createPage(count, currentPage, pageSize);
					books = bookImpl.selectByPublisher(choose, page.getBeginIndex(), pageSize);
				}
				break;
			default :
				List<String> singleString = StringUtils.getSingleString(choose);
				
				count = bookImpl.countFuzzy(singleString);
				if(count > 0){
					page = PageUtils.createPage(count, currentPage, pageSize);
					books = bookImpl.selectFuzzy(singleString, page.getBeginIndex(), pageSize);
				}
				break;
		}
		
		if (books.size() > 0) {
			globalMap.put("list", books);
			globalMap.put("page", page);
			globalMap.put(STATUS_CODE, SUCCESS);
		} else if (books.size() == 0) {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		} else {
			globalMap.put(STATUS_CODE, SELECT_WR);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	/**
	 * @Author shisfish
	 * @Date 2016年1月5日 下午4:14:36
	 * @Descrition : 模糊查询书类信息（分页）
	 * @param choose
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	@GET
	@Path("selectFuzzy/{choose}/{currentPage}/{pageSize}")
	@Produces("application/json; charset=utf-8")
	public String selectFuzzy(@PathParam("choose") String choose, 
			@PathParam("currentPage") int currentPage, @PathParam("pageSize") int pageSize) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		List<String> singleString = StringUtils.getSingleString(choose);
		
		int count = bookImpl.countFuzzy(singleString);
		
		if (count > 0) {
			Page page = PageUtils.createPage(count, currentPage, pageSize);
			
			List<Book> books = bookImpl.selectFuzzy(singleString, page.getBeginIndex(), pageSize);
			
			globalMap.put("list", books);
			
			globalMap.put("page", page);
			
			globalMap.put(STATUS_CODE, SUCCESS);
		} else if (count == 0) {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		}
		else {
			globalMap.put(STATUS_CODE, SELECT_WR);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	/**
	 * @Author shisfish
	 * @Date 2016年1月8日 下午4:23:11
	 * @Descrition : 模糊查询书类信息（分页）（含书本数量信息）
	 * @param choose
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	@GET
	@Path("selectFuzzyLess/{choose}/{currentPage}/{pageSize}")
	@Produces("application/json; charset=utf-8")
	public String selectFuzzyLess(@PathParam("choose") String choose, 
			@PathParam("currentPage") int currentPage, @PathParam("pageSize") int pageSize) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		List<String> singleString = StringUtils.getSingleString(choose);
		
		int count = bookImpl.countFuzzy(singleString);
		
		if (count > 0) {
			Page page = PageUtils.createPage(count, currentPage, pageSize);
			
			List<Map<String, Object>> books = bookImpl.selectFuzzyLess(singleString, page.getBeginIndex(), pageSize);
			
			if (books.size() > 0) {
				globalMap.put("list", books);
				globalMap.put("page", page);
				globalMap.put(STATUS_CODE, SUCCESS);
			} else if (books.size() == 0) {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			} else {
				globalMap.put(STATUS_CODE, SELECT_WR);
			}
		} else if (count == 0) {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		}
		else {
			globalMap.put(STATUS_CODE, SELECT_WR);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	/**
	 * @author shisfish
	 * @date 2016年2月4日 下午7:55:59
	 * @Description 通过 isbn 查找书的信息
	 * @param isbn
	 * @return
	 */
	@GET
	@Path("allMessageByISBN/{isbn}")
	@Produces("application/json; charset=utf-8")
	public String allMessageByISBN(@PathParam("isbn") String isbn) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		List<Book> books = bookImpl.selectByKey(isbn);
		
		if (books.size() > 0) {
			globalMap.put("list", books);
			globalMap.put(STATUS_CODE, SUCCESS);
		} else if (books.size() == 0) {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		}
		else {
			globalMap.put(STATUS_CODE, SELECT_WR);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	/**
	 * @author shisfish
	 * @date 2016年2月20日 下午8:13:16
	 * @Description 修改书籍信息
	 * @param idadmin
	 * @param request
	 * @return
	 */
	@POST
	@Path("updateABook/{idadmin}")
	@Produces("application/json; charset=utf-8")
	public String updateABook(@PathParam("idadmin") String idadmin, @Context HttpServletRequest request) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		if (Login.isLoginAdmin(idadmin, request)) {
		
			JSONArray customjsArr;
			// 取得所有文件和表单值
			JSONArray jsArr = fileUploadUtils.getRiverWayUpload(request);
			// 取得所有普通表单值
			customjsArr = new FormJsonArrUtil().getFormJsonArr(jsArr, "form");
	
			// 获取form表单数据
			String code = jsonValueUtils.getValue(customjsArr, "code");
			String isbn = jsonValueUtils.getValue(customjsArr, "isbn");
	
			String isbn10 = "";
			String isbn13 = "";
			isbn = isbn.replaceAll("-", "");
			if (isbn.length() == 10) {
				isbn10 = isbn;
				isbn13 = isbn + "123";ChangeISBNUtils.getISBN13(isbn);
			} else {
				isbn10 = isbn.substring(0, 10);
				isbn13 = isbn;
			}
	
			String title = jsonValueUtils.getValue(customjsArr, "title");
			String subtitle = jsonValueUtils.getValue(customjsArr, "subtitle");
			String title_foreign = jsonValueUtils.getValue(customjsArr,
					"title_foreign");
			String author = jsonValueUtils.getValue(customjsArr, "author");
			String publisher = jsonValueUtils.getValue(customjsArr, "publisher");
			String pubaddress = jsonValueUtils.getValue(customjsArr, "pubaddress");
			String pubdate = jsonValueUtils.getValue(customjsArr, "pubdate");
			String pages = jsonValueUtils.getValue(customjsArr, "pages");
			String size = jsonValueUtils.getValue(customjsArr, "size");
			String keyword = jsonValueUtils.getValue(customjsArr, "keyword");
			String summary = jsonValueUtils.getValue(customjsArr, "summary");
			String category = jsonValueUtils.getValue(customjsArr, "category");
			String language = jsonValueUtils.getValue(customjsArr, "language");
			String price = jsonValueUtils.getValue(customjsArr, "price");
			String series_name = jsonValueUtils.getValue(customjsArr, "series_name");
			String enclosure = jsonValueUtils.getValue(customjsArr, "enclosure");
			String cn = jsonValueUtils.getValue(customjsArr, "cn");
			String period = jsonValueUtils.getValue(customjsArr, "period");
			String unit = jsonValueUtils.getValue(customjsArr, "unit");
			String classification = jsonValueUtils.getValue(customjsArr,
					"classification");
			String binding = jsonValueUtils.getValue(customjsArr, "binding");
			String translator = jsonValueUtils.getValue(customjsArr, "translator");
	
			Book book = new Book(code, isbn10, isbn13, title, subtitle,
					title_foreign, author, publisher, pubaddress, pubdate, pages,
					size, keyword, summary, category, language, price, series_name,
					enclosure, cn, period, unit, classification, binding,
					translator);
	
			int flag = bookImpl.updateOne(book);
	
			if (flag == 0) {
				globalMap.put(STATUS_CODE, INSERT_WR);
			} else {
				
				// 新增一本书类的时候 添加书的数量信息
				BookMessage bookMessage = new BookMessage(isbn13, 0, 0, 0, 0, 0);
				
				bookMessageImpl.insertOne(bookMessage);
				
				globalMap.put(STATUS_CODE, SUCCESS);
			}
		}
		else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		return toJsonUtils.map2Json(globalMap);

	}
	
	/**
	 * @author shisfish
	 * @date 2016年2月20日 下午5:05:17
	 * @Description 通过isbn 删除书类
	 * @param idadmin
	 * @param request
	 * @param isbn
	 * @return
	 */
	@POST
	@Path("deleteByISBN/{idadmin}")
	@Produces("application/json; charset=utf-8")
	public String deleteByISBN(@PathParam("idadmin") String idadmin, @Context HttpServletRequest request, @FormParam("isbn") String isbn) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		if (Login.isLoginAdmin(idadmin, request)) {
			int flag = bookImpl.deleteOneByKey(isbn);
			
			if (flag == 0) {
				globalMap.put(STATUS_CODE, DELETE_WR);
			} else {
				globalMap.put(STATUS_CODE, SUCCESS);
			}
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	/**
	 * @author shisfish
	 * @date 2016年2月20日 下午8:12:00
	 * @Description 查询全部的书籍（分页）
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	@GET
	@Path("selectAllPage/{currentPage}/{pageSize}")
	@Produces("application/json; charset=utf-8")
	public String selectAllPage(@PathParam("currentPage") int currentPage, @PathParam("pageSize") int pageSize) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		int count = bookImpl.countAll();
		
		if (count > 0) {
			Page page = PageUtils.createPage(count, currentPage, pageSize);
			List<Book> books = bookImpl.selectAllPage(page.getBeginIndex(), pageSize);
			
			if (books.size() > 0) {
				globalMap.put(STATUS_CODE, SUCCESS);
				globalMap.put("page", page);
				globalMap.put("list", books);
			} else if (books.size() == 0) {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			} else {
				globalMap.put(STATUS_CODE, SELECT_WR);
			}
		} else if (count == 0) {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		} else {
			globalMap.put(STATUS_CODE, SELECT_WR);
		}

		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("selectByISBN/{isbn}")
	@Produces("application/json; charset=utf-8")
	public String selectByISBN(@PathParam("isbn") String isbn) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		List<Book> books =  bookImpl.selectByISBN(isbn.replaceAll("-", ""));
		if (books.size() > 0) {
			globalMap.put("list", books);
			globalMap.put(STATUS_CODE, SUCCESS);
		} else if (books.size() == 0) {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		} else {
			globalMap.put(STATUS_CODE, SELECT_WR);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("selectJournal/{choose}/{currentPage}/{pageSize}")
	@Produces("application/json; charset=utf-8")
	public String selectJournal(@PathParam("choose") String choose, 
			@PathParam("currentPage") int currentPage, @PathParam("pageSize") int pageSize) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		choose = choose.replaceAll("-", "");
		List<String> singleString = StringUtils.getSingleString(choose);
		int count = bookImpl.countJournal(singleString);
		
		// 期刊的查询方式：名称，
		
		if (count > 0) {
			
			Page page = PageUtils.createPage(count, currentPage, pageSize);
			
			List<Book> books = bookImpl.selectJournal(singleString, page.getBeginIndex(), pageSize);
			
			globalMap.put("list", books);
			
			globalMap.put("page", page);
			
			globalMap.put(STATUS_CODE, SUCCESS);
		} else if (count == 0) {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		}
		else {
			globalMap.put(STATUS_CODE, SELECT_WR);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("selectJournalDetail/{isbn}")
	@Produces("application/json; charset=utf-8")
	public String selectJournalDetail(@PathParam("isbn") String isbn) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		Book book =  bookImpl.selectJournalDetail(isbn.replaceAll("-", ""));
		if (book != null) {
			globalMap.put("journal", book);
			globalMap.put(STATUS_CODE, SUCCESS);
		} else {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("selectNewAddPage/{type}/{currentPage}/{pageSize}")
	@Produces("application/json; charset=utf-8")
	public String selectNewAddPage(@PathParam("type") int type, @PathParam("currentPage") int currentPage, @PathParam("pageSize") int pageSize) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		int count = bookImpl.countNewAdd(type);
		
		if (count > 0) {
			Page page = PageUtils.createPage(count, currentPage, pageSize);
			List<Book> books = bookImpl.selectNewAddPage(type, page.getBeginIndex(), pageSize);
			
			if (books.size() > 0) {
				globalMap.put(STATUS_CODE, SUCCESS);
				globalMap.put("page", page);
				globalMap.put("list", books);
			} else if (books.size() == 0) {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			} else {
				globalMap.put(STATUS_CODE, SELECT_WR);
			}
		} else if (count == 0) {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		} else {
			globalMap.put(STATUS_CODE, SELECT_WR);
		}

		return toJsonUtils.map2Json(globalMap);
	}
	

}
